local P = ClassFromFile(...);

function P:_init()
	if not self._ctrl then
		return
	end

	local player = nil
	for k,v in pairs(self._ctrl) do
		if string.sub(k, 1, 4) == "Net_" or string.sub(k, 1, 4) == "set_" then
			if not player then
				player = KBEngineLua.player()
			end
			self:AddNetListener(k, v, player)
		end
	end
	self.timers = {}
end
function P:ReLoadInit()
	if self.removeListenerFuns then
		for i = #self.removeListenerFuns,1,-1 do
			self.removeListenerFuns[i]()
			self.removeListenerFuns[i] = nil
		end
	end
	self:_init()
end

-- 跟随窗口销毁
-- loop -1 一直循环
function P:AddTimer(func, delay, duration, loop)
	local timer = TimerEx.New(func, delay, duration, loop)
	timer:Start()
	table.insert(self.timers, timer)
	return timer
end

-- 显示资源条
function P:ShowResourceTitle(resourceTitles)
	self:AddNetListener("set_gold", P.set_gold, KBEngineLua.player())
	self:AddNetListener("set_diamond", P.set_diamond, KBEngineLua.player())
	self:AddNetListener("set_arenaHonor", P.set_diamond, KBEngineLua.player())
	self:AddNetListener("set_exploreShopCoin", P.set_diamond, KBEngineLua.player())
	self:AddNetListener("set_exhibitGold", P.set_diamond, KBEngineLua.player())
	self:AddNetListener("set_guildCoin", P.set_diamond, KBEngineLua.player())
	self:AddNetListener("set_hotelCoin", P.set_diamond, KBEngineLua.player())
	self:AddNetListener("set_endlessPoint", P.set_diamond, KBEngineLua.player())
	self:AddNetListener("set_homeCoin", P.set_diamond, KBEngineLua.player())
	self:AddNetListener("set_drawCardCoin", P.set_drawCardCoin, KBEngineLua.player())


	self.resourceTitles = resourceTitles
	self.resourceTitleCallback = CreateCallBack(P.ResourceTitleCallback, self)
	ObjectPool.LoadAssetAsync("prefab/ui/ResourceGroup", self.resourceTitleCallback)
end

-- 隐藏资源条
function P:ShowAndHideResourceTitle(isShow)
	if not self.resourceGroup or self.resourceGroup:Equals(nil) then
		return
	end
	self.resourceGroup.gameObject:SetActive(isShow)
end
function P:UpdateResourcesTitle(resourceTitles)
	if not self.resourceGroup or self.resourceGroup:Equals(nil) then
		return
	end
	self.resourceTitles = resourceTitles
	self:ResourceTitleCallback(nil,self.resourceGroup.gameObject)
end
function P:ResourceTitleCallback(prefab, object)
	local titleObj = object
	if not self._funObj or self._funObj:Equals(nil) then
		GameObject.Destroy(titleObj)
		return
	end
	titleObj.transform:SetParent(self._funObj.transform)
	--titleObj.transform.localPosition = Vector3Ex.zero
	titleObj.transform.localScale = Vector3Ex.one
	self.resourceGroup = GetComponentNew(titleObj, ResourceGroup)
	local curDepth = self._funObj:GetComponent(typeof(UIPanel)).depth + 60
	titleObj:GetComponent(typeof(UIPanel)).depth = curDepth
	--初始全关闭
	for i = 0, self.resourceGroup.resources.Length - 1 do
		local res = CreateDataContainerProxy(self.resourceGroup.resources[i])
		res.resObj:SetActive(false)
	end
	local player = KBEngineLua.player()
	for i = 1, #self.resourceTitles do
		resourceType = self.resourceTitles[i]
		for j = 0, self.resourceGroup.resources.Length do
			local res = CreateDataContainerProxy(self.resourceGroup.resources[j])
			if res.resType == resourceType then
				res.resObj:SetActive(true)
				if resourceType == ResourceType.GOLD then
					res.resText.text = NumFormat(player.gold)
					UIEventListener.Get(res.addBtn).onClick = UIEventListener.VoidDelegate(P.OnClickAddGold, self)
				elseif resourceType == ResourceType.DIAMOND then
					res.resText.text = player.diamond
					UIEventListener.Get(res.addBtn).onClick = UIEventListener.VoidDelegate(P.OnClickAddDiamond, self)
				elseif resourceType == ResourceType.ARENAHONOR then
					res.resText.text = NumFormat(player.arenaHonor)
				elseif resourceType == ResourceType.EXPLORE then
					res.resText.text = NumFormat(player.exploreShopCoin)
				elseif resourceType == ResourceType.EXHIBIT then
					res.resText.text = NumFormat(player.exhibitGold)
				elseif resourceType == ResourceType.GUILD then
					res.resText.text = NumFormat(player.guildCoin)
				elseif resourceType == ResourceType.HOTEL then
					res.resText.text = NumFormat(player.hotelCoin)
				elseif resourceType == ResourceType.ENDLESS then
					res.resText.text = NumFormat(player.endlessPoint)
				elseif resourceType == ResourceType.HOME then
					res.resText.text = NumFormat(player.homeCoin)
				elseif resourceType == ResourceType.DRAWCARD then
					res.resText.text = NumFormat(player.drawCardCoin)
				end
				break
			end
		end
	end
	self.resourceGroup.transform:Find("Table"):GetComponent(typeof(UITable)):Reposition() --- 不满回弹到顶部
	self.resourceGroup.transform:Find("Table"):GetComponent(typeof(UITable)).repositionNow = true;
end

function P.OnClickAddGold(self)
	-- RootWindowManager.instance:ShowWindow("ShopWindow",true,true):Init()
end
function P.OnClickAddDiamond(self)
	-- RootWindowManager.instance:ShowWindow("ShopWindow",true,true):Init()
end

-- 金币发生变化
function P:set_gold(gold)
	if self.resourceGroup == nil then
		return
	end
	local player = KBEngineLua.player()
	for j = 0, self.resourceGroup.resources.Length do
		local res = CreateDataContainerProxy(self.resourceGroup.resources[j])
		if res.resType == ResourceType.GOLD then
			res.resText.text = NumFormat(player.gold)
			break
		end
	end
end

-- 钻石发生变化
function P:set_diamond(diamond)
	if self.resourceGroup == nil then
		return
	end
	local player = KBEngineLua.player()
	for j = 0, self.resourceGroup.resources.Length do
		local res = CreateDataContainerProxy(self.resourceGroup.resources[j])
		if res.resType == ResourceType.DIAMOND then
			res.resText.text = player.diamond
			break
		end
	end
end

function P:set_arenaHonor(arenaHonor)
	if self.resourceGroup == nil then
		return
	end
	local player = KBEngineLua.player()
	for j = 0, self.resourceGroup.resources.Length do
		local res = CreateDataContainerProxy(self.resourceGroup.resources[j])
		if res.resType == ResourceType.ARENAHONOR then
			res.resText.text = player.arenaHonor
			break
		end
	end
end
function P:set_exploreShopCoin(exploreShopCoin)
	if self.resourceGroup == nil then
		return
	end
	local player = KBEngineLua.player()
	for j = 0, self.resourceGroup.resources.Length do
		local res = CreateDataContainerProxy(self.resourceGroup.resources[j])
		if res.resType == ResourceType.EXPLORE then
			res.resText.text = player.exploreShopCoin
			break
		end
	end
end
function P:set_exhibitGold(exhibitGold)
	if self.resourceGroup == nil then
		return
	end
	local player = KBEngineLua.player()
	for j = 0, self.resourceGroup.resources.Length do
		local res = CreateDataContainerProxy(self.resourceGroup.resources[j])
		if res.resType == ResourceType.EXHIBIT then
			res.resText.text = player.exhibitGold
			break
		end
	end
end
function P:set_guildCoin(set_guildCoin)
	if self.resourceGroup == nil then
		return
	end
	local player = KBEngineLua.player()
	for j = 0, self.resourceGroup.resources.Length do
		local res = CreateDataContainerProxy(self.resourceGroup.resources[j])
		if res.resType == ResourceType.GUILD then
			res.resText.text = player.guildCoin
			break
		end
	end
end
function P:set_hotelCoin(hotelCoin)
	if self.resourceGroup == nil then
		return
	end
	local player = KBEngineLua.player()
	for j = 0, self.resourceGroup.resources.Length do
		local res = CreateDataContainerProxy(self.resourceGroup.resources[j])
		if res.resType == ResourceType.HOTEL then
			res.resText.text = player.hotelCoin
			break
		end
	end
end
function P:set_endlessPoint(endlessPoint)
	if self.resourceGroup == nil then
		return
	end
	local player = KBEngineLua.player()
	for j = 0, self.resourceGroup.resources.Length do
		local res = CreateDataContainerProxy(self.resourceGroup.resources[j])
		if res.resType == ResourceType.ENDLESS then
			res.resText.text = player.endlessPoint
			break
		end
	end
end
function P:set_homeCoin(homeCoin)
	if self.resourceGroup == nil then
		return
	end
	local player = KBEngineLua.player()
	for j = 0, self.resourceGroup.resources.Length do
		local res = CreateDataContainerProxy(self.resourceGroup.resources[j])
		if res.resType == ResourceType.HOME then
			res.resText.text = player.homeCoin
			break
		end
	end
end
function P:set_drawCardCoin(drawCardCoin)
	if self.resourceGroup == nil then
		return
	end
	local player = KBEngineLua.player()
	for j = 0, self.resourceGroup.resources.Length do
		local res = CreateDataContainerProxy(self.resourceGroup.resources[j])
		if res.resType == ResourceType.DRAWCARD then
			res.resText.text = player.drawCardCoin
			break
		end
	end
end



local function _Broadcast(self, fun, ...)
	if not self.broadcastListeners then
		return
	end

	local listeners = self.broadcastListeners[fun]
	if not listeners then
		return
	end

	local args = { ... }
	for k,v in ipairs(listeners) do
		v(unpack(args))
	end
end

function P:RemoveNetListener(removeListenerFun)
	-- if not player or not listener then
	-- 	return
	-- end
	
	-- if not player._Broadcast then
	-- 	return
	-- end

	-- if not player.broadcastListeners then
	-- 	return
	-- end

	-- local funListeners = player.broadcastListeners[k]
	-- if not funListeners then
	-- 	return
	-- end

	-- local foundPos = table.find(funListeners, function (k, v) return v == listener end)
	-- table.remove(funListeners, foundPos)

	if not removeListenerFun then
		return
	end

	removeListenerFun()

	if not self.removeListenerFuns then
		return
	end

	local foundPos = table.find(self.removeListenerFuns, function (k, v) return v == removeListenerFun end)
	table.remove(self.removeListenerFuns, foundPos)
end

function P:AddNetListener(k, f, player)
	if not player then
		return
	end
	
	if not player._Broadcast then
		player._Broadcast = _Broadcast
	end

	if not player.broadcastListeners then
		player.broadcastListeners = {}
	end


	-- new
	local funListeners = player.broadcastListeners[k]
	if not funListeners then
		funListeners = {}
		player.broadcastListeners[k] = funListeners

		local oldFun = player[k]
		local fun = function (self, ...)
			local args = { ... }
			if oldFun then
				oldFun(self, unpack(args))
			end

			for k, v in ipairs(funListeners) do
				v(unpack(args))
			end
		end

		player[k] = fun
		player.broadcastListeners[fun] = funListeners
	end

	-- old
	-- local funListeners = player.broadcastListeners[k]
	-- local hasFunListeners = funListeners
	-- if not hasFunListeners then
	-- 	funListeners = {}
	-- 	player.broadcastListeners[k] = funListeners
	-- end

	-- local fun = player[k]
	-- if not hasFunListeners then
	-- 	local oldFun = player[k]
	-- 	fun = function (self, ...)
	-- 		local args = { ... }
	-- 		if oldFun then
	-- 			oldFun(self, unpack(args))
	-- 		end

	-- 		for k, v in ipairs(funListeners) do
	-- 			v(unpack(args))
	-- 		end
	-- 	end

	-- 	player[k] = fun
	-- 	player.broadcastListeners[fun] = funListeners
	-- end

	local listener = CreateCallBack(f, self)
	table.insert(funListeners, listener)

	local removeListenerFun = nil
	removeListenerFun = function ()
		local foundPos = table.find(funListeners, function (k, v) return v == listener end)
		table.remove(funListeners, foundPos)
	end

	if not self.removeListenerFuns then
		self.removeListenerFuns = {}
	end

	table.insert(self.removeListenerFuns, removeListenerFun)

	return removeListenerFun
end



function P:OnEnable()
	if not self.enableListeners then
		return
	end

	for _,v in ipairs(self.enableListeners) do
		v:OnEnable()
	end
end

function P:OnDisable()
	if not self.enableListeners then
		return
	end

	for _,v in ipairs(self.enableListeners) do
		v:OnDisable()
	end
end

function P:Start()
end

function P:OnDestroy()
	if self.removeListenerFuns then
		for i = #self.removeListenerFuns,1,-1 do
			self.removeListenerFuns[i]()
			self.removeListenerFuns[i] = nil
		end
	end
	if self.timers then
		for i, timer in pairs(self.timers) do
			timer:Stop()
		end
	end

	table.clear(self)
end

function P:AddEnableListener(listener)
	if not listener then
		return
	end

	if not self.enableListeners then
		self.enableListeners = {}
	end

	local pos = table.find(self.enableListeners, function(k, v) return listener == v end);
	if pos then
		return
	end

	table.insert(self.enableListeners, listener);
end

function P:RemoveEnableListener(listener)
	if not listener then
		return
	end

	if not self.enableListeners then
		return
	end

	local pos = table.find(self.enableListeners, function(k, v) return listener == v end);
	table.remove(self.enableListeners, pos);
end